import java.util.Random;

/**
* The Action Class provides a base, straight-damage action, such as a Standard
* Attack.  It is designed to be inherited from by other at-will, encounter, and
* daily powers.
* <P>
* There are a few tools provided for custom classes that wish to extend Action
* (which is probably just about every attack).
* <UL>
*		<LI> The Initialize Method provides a simple method which can be invoked from
*			your constructor
*		<LI> The doExActionSuccess method is invoked after the baseline damage is
* 		applied to the target affected by doAction().  Secondary effects can
*			be added here
*		<LI> The doExActionFail method is invoked if the doAction attack fails.
*		</UL>See Cleave.java for an example of extending Action
*/
public class Action
{
	/** Minimum damage of the attack (assumes modifiers from strength, magic
			etc.  3d6 damage would provide 3 for this */
	protected int iMinDmg;
	/** Maximum damage of the attack (assumes modifiers from strength, magic
			etc.  3d6 damage would provide 18 for this */
	protected int iMaxDmg;
	/** What stat is attacked,0 = AC, 1=Ref, 2=Wil, 3 =Fort*/
	protected int iAtkStat;
	/** To-Hit Bonus, with magic, and other stats figured in.*/
	protected int iBonus;
	protected Random rGenerator;
	//constructors kept simple for inheritance
	/** Default Constructor, makes an action that does 1 point of damage, 
			attacking AC, with no bonus to hit */ 
	public Action()
	{
		Initialize(1,1,0,0);
	}
	/** Detailed Constructor
	* 	@param iMyMin Minimum Damage of the attack
	*		@param iMyMax Maximum Damage of the attack
	*		@param iMyAttacks What defense the target attacks(0 = AC, 1=Ref, 2=Wil, 
	*											3 =Fort
	* 	@param iMyBonus Total Bonus to Hit (magic and stat bonuses inclusive)
	*/
	public Action(int iMyMin, int iMyMax, int iMyAttacks, int iMyBonus)
	{
		Initialize(iMyMin, iMyMax, iMyAttacks, iMyBonus);
	}
	/** Initialize is called by the Constructor, meant to simplify inheritance
	* 	@param iMyMin Minimum Damage of the attack
	*		@param iMyMax Maximum Damage of the attack
	*		@param iMyAttacks What defense the target attacks(0 = AC, 1=Ref, 2=Wil, 
	*											3 =Fort
	* 	@param iMyBonus Total Bonus to Hit (magic and stat bonuses inclusive)
	*/
	protected void Initialize(int iMyMin, int iMyMax, int iMyAttacks, int iMyBonus)
	{
		setMin(iMyMin);
		setMax(iMyMax);
		setAttacks(iMyAttacks);
		setBonus(iMyBonus);
		rGenerator = new Random();
	}
	/** setMin is used to set the minimum damage of the attack.
	*/
	public void setMin(int iMyMin)
	{
		iMinDmg = iMyMin;
	}
	/** setMax is used to set the maximum damage of the attack.
	*/
	public void setMax(int iMyMax)
	{
		iMaxDmg = iMyMax;
	}
	/** setAttacks is used to set the defensive stat targeted by the attack.
	* .0 = AC, 1=Ref, 2=Wil, 3 =Fort
	*/
	public void setAttacks(int iMyAttacks)
	{
		iAtkStat = iMyAttacks;
	}
	/** setBonus is used to set Total Bonus to Hit (magic and stat bonuses 
	*		inclusive)
	*/
	public void setBonus(int iMyBonus)
	{
		iBonus = iMyBonus;
	}
	/** doAction performs an attack on Combatant Target, determining whether it
	*		hits, and how much it does, then applies it to Target
	*		@see doExActionSuccess
	*		@see doExActionFail
	*/
	public void doAction(Combatant Target)
	{
		int iTargetNum = 0;
		switch(iAtkStat)
		{
			case 0: iTargetNum = Target.getAC(); break;
			case 1: iTargetNum = Target.getReflex(); break;
			case 2: iTargetNum = Target.getWill(); break;
			case 3: iTargetNum = Target.getFort(); break;
		}				
		
		int iRoll = rGenerator.nextInt(20);
		iRoll++; //1-20
		iRoll += iBonus;
		
		if(iRoll >= iTargetNum)
		{
			int iMyDamage = 1; // if this is an attack that does 1
			if(iMaxDmg>iMinDmg)
			{
				iMyDamage = rGenerator.nextInt(iMaxDmg-iMinDmg);
				iMyDamage += iMinDmg + 1;
			}
			Target.takeDamage(iMyDamage);
			doExActionSuccess(Target);
		}
		else
		{
			doExActionFail(Target);
			String sMessage = Target.getName() + " is missed!";
			Target.getParty().getEncounter().printLog(sMessage);
		}
	}
	/** doExActionSuccess is called in the success condition of doAction.
	*		It is meant to make applying secondary effects easy to apply with
	*		inheriting classes.
	*		It is invoked with whatever Combatant was handed to doAction
	*/
	protected void doExActionSuccess(Combatant Target)
	{
	}
	/** doExActionFail is called in the failure condition of doAction.
	*		It is meant to make applying secondary effects easy to apply with
	*		inheriting classes.
	*		It is invoked with whatever Combatant was handed to doAction
	*/
	protected void doExActionFail(Combatant Target)
	{
	}
}
